package cn.stylefeng.guns.modular.car.controller;

import cn.stylefeng.guns.modular.car.entity.Car;
import cn.stylefeng.guns.modular.car.pojo.request.CarRequest;
import cn.stylefeng.guns.modular.car.pojo.response.CarResponse;
import cn.stylefeng.guns.modular.car.service.CarService;
import cn.stylefeng.guns.modular.car.service.UserService;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 控制器
 *
 * @author xbz
 * @date 2023/04/07 14:21
 */
@RestController
@ApiResource(name = "车辆管理")
public class CarController {

    @Resource
    private CarService carService;

    /**
     * 添加
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @PostResource(name = "添加", path = "/car/add",requiredPermission = false,requiredLogin = false)
    public ResponseData<Car> add(@RequestBody @Validated(CarRequest.add.class) CarRequest carRequest) {
        carService.add(carRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @PostResource(name = "删除", path = "/car/delete",requiredPermission = false,requiredLogin = false)
    public ResponseData<?> delete(@RequestBody @Validated(CarRequest.delete.class) CarRequest carRequest) {
        carService.del(carRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @PostResource(name = "编辑", path = "/car/edit",requiredPermission = false,requiredLogin = false)
    public ResponseData<?> edit(@RequestBody @Validated(CarRequest.edit.class) CarRequest carRequest) {
        carService.edit(carRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看详情
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @GetResource(name = "查看详情", path = "/car/detail",requiredPermission = false,requiredLogin = false)
    public ResponseData<Car> detail(@Validated(CarRequest.detail.class) CarRequest carRequest) {
        return new SuccessResponseData<>(carService.detail(carRequest));
    }

    /**
     * 获取列表
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @GetResource(name = "获取列表", path = "/car/list",requiredPermission = false,requiredLogin = false)
    public ResponseData<List<Car>> list(CarRequest carRequest) {
        return new SuccessResponseData<>(carService.findList(carRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @GetResource(name = "分页查询", path = "/car/page",requiredPermission = false,requiredLogin = false)
    public ResponseData<PageResult<Car>> page(CarRequest carRequest) {
        return new SuccessResponseData<>(carService.findPage(carRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @GetResource(name = "分页查询", path = "/car/selectPage",requiredPermission = false,requiredLogin = false)
    public ResponseData<PageResult<CarResponse>> selectPage(String name, Integer pageNo, Integer pageSize) {
        return new SuccessResponseData<>(carService.selectPage(name, pageNo, pageSize));
    }


    @Resource
    private UserService userService;
    /**
     * 获取列表（带分页）
     *
     * @author xbz
     * @date 2023/04/07 14:21
     */
    @GetResource(name = "获取用户列表", path = "/car/userList",requiredPermission = false,requiredLogin = false)
    public ResponseData<List> userList() {
        return new SuccessResponseData<>(userService.list(null));
    }

    @PostResource(name = "导入车辆",path = "/api/car/import",requiredPermission = false,requiredLogin = false)
    public void importExcel(MultipartFile file, HttpServletResponse response){
        carService.importExcel(file, response);
    }
    @GetResource(name = "导出车辆",path = "/api/car/export",requiredPermission = false,requiredLogin = false)
    public void exportExcel(HttpServletResponse response) throws IOException {
        carService.exportExcel(response);
    }

    @GetResource(name = "车辆统计",path = "/car/carCount",requiredPermission = false,requiredLogin = false)
    public ResponseData<List> carCount(){
        return new SuccessResponseData<>(carService.carCount());
    }
}
